package ctgu;

import java.math.BigDecimal;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.zone.ZoneRules;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Desc.
 * @return {@link null}
 * @author JUYI
 */

public class ZonedTime {

    /**
     * @datayype logger
     * @identifier LOGGER
     */

    private static final Logger LOGGER =
            LoggerFactory.getLogger(ZonedTime.class);
    /**
     * @datayype ZonedDateZonedTime
     * @identifier startingTime
     */

    private static ZonedDateTime startingTime;
    /**
     * @datayype ZonedDateTime
     * @identifier endingTime
     */

    private static ZonedDateTime endingTime;

    /**
     * Desc.
     * @param startTime 开始时间
     * @param endTime 结束时间
     */
    public ZonedTime(final ZonedDateTime startTime,
                final ZonedDateTime endTime) {
        this.startingTime = startTime;
        this.endingTime = endTime;
    }
    /**
     * Desc.
     * @param
     * @return {@link long}
     * @author JUYI
     * @date 2022/4/11 0:46
     */


    public final long getTime() {
        long minute = 0;
        long between;

        ZoneId zoneId = startingTime.getZone();
        ZoneRules zoneRules = zoneId.getRules();

        Boolean isDstOfStart = zoneRules.
                isDaylightSavings(startingTime.toInstant());
        Boolean isDstOfEnd = zoneRules.
                isDaylightSavings(endingTime.toInstant());

        LOGGER.info("start:{},夏令时:{},end:{},夏令时:{}",
                startingTime.toLocalDateTime(),
                isDstOfStart,
                endingTime.toLocalDateTime(),
                isDstOfEnd
        );

        between = endingTime.toEpochSecond() - startingTime.toEpochSecond();

        final int min = 60;
        final int lessmin = 59;
        final int maxMinute = 1800;
        final int errorMinute = 0;
        if (between < 0) {
            minute = between / min;
        } else {
            minute = (between + lessmin) / min;
        }

        LOGGER.info("between:{}, minutes:{}", between, minute);

        final int hour = 2;
        final int month = 11;

        if (minute > maxMinute || minute < errorMinute) {
            if (endingTime.getHour() == hour
                    && startingTime.getHour() == hour
                    && startingTime.getMonthValue() == month
                    && endingTime.getMonthValue() == month
                    && !isDstOfStart && !isDstOfEnd) {
                return minute + min;
            } else {
                return -1;
            }
        } else {
            return minute;
        }
    }

    /**
     * Desc.
     * @param
     * @return {@link double}
     * @author JUYI
     * @date 2022/4/11 0:46
     */


    public final double getprice() {
        double sumprice = 0;
        long sumtime = getTime();
        final int specialMinute = 20;
        final double lowCost = 0.05;
        final double highCost = 0.10;
        if (sumtime <= specialMinute) {
            sumprice = (double) sumtime * lowCost;
        } else {
            sumprice += 1;
            sumprice += ((double) sumtime - (double) specialMinute) * highCost;
        }
        BigDecimal two = new BigDecimal(sumprice);
        double nsumprice = two.setScale(2, BigDecimal.ROUND_HALF_UP)
                .doubleValue();
        LOGGER.info("sumprice:{}", nsumprice);

        return nsumprice;
    }
}
